import { createRouter, createWebHistory } from 'vue-router'
const routes = [
  {
    path: '/user/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "index" */ '@/views/user/login.vue'),
  },
  {
    path: '/',
    name: 'index',
    component: () => import(/* webpackChunkName: "index" */ '../layouts/index.vue'),
    redirect: '/home/index',
    children: [
      {
        path: '/home/index',
        name: 'homeIndex',
        component: () => import(/* webpackChunkName: "homeIndex" */ '@/views/home/index.vue'),
        redirect: '/home',
        meta: {
          title: '首页'
        },
        children: [
          {
            path: '/home',
            name: 'homeIndex',
            component: () => import(/* webpackChunkName: "home" */ '@/views/home/home.vue'),
            meta: {
              title: '首页'
            },
          }
        ]
      },
      {
        path: '/basicInfo/index',
        name: 'basicInfoIndex',
        component: () => import(/* webpackChunkName: "basicInfoIndex" */ '@/views/basicInfo/index.vue'),
        redirect: '/basicInfo/productInfo',
        meta: {
          title: '基础信息'
        },
        children: [
          {
            path: '/basicInfo/productInfo',
            name: 'productInfo',
            component: () => import(/* webpackChunkName: "productInfo" */ '@/views/basicInfo/productInfo.vue'),
            meta: {
              title: '产品清单'
            },
          },
          {
            path: '/basicInfo/materialInfo',
            name: 'materialInfo',
            component: () => import(/* webpackChunkName: "materialInfo" */ '@/views/basicInfo/materialInfo.vue'),
            meta: {
              title: '物料清单'
            },
          },
          {
            path: '/basicInfo/productBom',
            name: 'productBom',
            component: () => import(/* webpackChunkName: "productBom" */ '@/views/basicInfo/productBom.vue'),
            meta: {
              title: '产品Bom'
            },
          }
        ]
      },
      {
        path: '/purchase/index',
        name: 'purchaseIndex',
        component: () => import(/* webpackChunkName: "purchaseIndex" */ '@/views/purchase/index.vue'),
        redirect: '/purchase/purchase',
        meta: {
          title: '采购订单'
        },
        children: [
          {
            path: '/purchase/purchase',
            name: 'purchase',
            component: () => import(/* webpackChunkName: "purchase" */ '@/views/purchase/purchase.vue'),
            meta: {
              title: '采购订单'
            },
          }
        ]
      },
      {
        path: '/store/index',
        name: 'storeIndex',
        component: () => import(/* webpackChunkName: "storeIndex" */ '@/views/store/index.vue'),
        redirect: '/store/fromCheck',
        meta: {
          title: '仓库管理'
        },
        children: [
          {
            path: '/store/fromCheck',
            name: 'fromCheck',
            component: () => import(/* webpackChunkName: "fromCheck" */ '@/views/store/fromCheck.vue'),
            meta: {
              title: '来料检验'
            },
          },
          {
            path: '/store/inManage',
            name: 'inManage',
            component: () => import(/* webpackChunkName: "inManage" */ '@/views/store/inManage.vue'),
            meta: {
              title: '入库管理'
            },
          },
          {
            path: '/store/storeManage',
            name: 'storeManage',
            component: () => import(/* webpackChunkName: "storeManage" */ '@/views/store/storeManage.vue'),
            meta: {
              title: '库存管理'
            },
          },
          {
            path: '/store/outManage',
            name: 'outManage',
            component: () => import(/* webpackChunkName: "outManage" */ '@/views/store/outManage.vue'),
            meta: {
              title: '出库管理'
            },
          }
        ]
      },
    ]
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
const whiteList = ['/user/login']

router.beforeEach((to, from, next) => {
  // console.log('to.path', to.path)
  if (localStorage.getItem('ACCESS_TOKEN')) {
    next()
  }
  else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next({ path: '/user/login' })
    }
  }
})

export default router
